﻿using System;
using System.ComponentModel;
using System.Windows;
using Labthink.PCSoft.UniversalHost4.Utility;
using SystemSettings;
using Labthink.PCSoft.UniversalHost4.DataAccess;
using Labthink.PCSoft.UniversalHost4.Model.Setting;

namespace Labthink.PCSoft.UniversalHost4.UserControls
{
    public partial class UcBaseInfo
    {
        private NotifyManager _notify;

        private BaseInfo _info;
        public string Title { get; set; } = LanguageManager.GetLanguageValue("BaseInfoSetting");

        public string PermissionCode { get; set; }


        public UcBaseInfo()
        {
            InitializeComponent();
            _info = Common.BaseInfo;
            ConfigAndSettings.BaseInfoChanged += BaseInfoChanged;
            BtnOk.Focus();
        }

        private void BaseInfoChanged(object sender, EventArgs e)
        {
            _info = ConfigAndSettings.GetBaseInfo();
            Common.BaseInfo = _info;
            FillBaseInfo();
        }

        private void UcBaseInfo_OnLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                return;
            }

            var w = Window.GetWindow(this);
            _notify = NotifyManager.InstanceManager(w);
            FillBaseInfo();
        }

        private void BtnOk_OnClick(object sender, RoutedEventArgs e)
        {
            _triggerButtonsStatus(false);

            _info.Name = TbDeviceName.Value;
            _info.Type = TbDeviceType.Value + "";
            _info.Device = TbDeviceCode.Value + "";
            _info.Year = TbProductionYear.Value + "";
            _info.Sn = TbProductionCode.Value + "";
            _info.DataShield = CbEnableDs.IsChecked ?? false;
            _info.Gmp = CbEnableGmp.IsChecked ?? false;
            _info.Unit = CbEnableUnitSys.IsChecked ?? false;
            _info.AutoReport = CbEnableAutoReport.IsChecked ?? false;
            if (!string.IsNullOrEmpty(IudTestId.Value)) _info.LastTestId = IudTestId.Value.ToInt();

            try
            {
                ConfigAndSettings.SetBaseInfo(_info);
                _info = ConfigAndSettings.GetBaseInfo();
                Common.BaseInfo = _info;
                var success = LanguageManager.GetLanguageValue("ActionSuccess");
                _notify.ShowSuccess(success);
                LogRepository.AddLog(LanguageManager.GetLanguageValue("OP-SetBaseInfo"), "Manage");
                _triggerButtonsStatus(true);
            }
            catch (Exception ex)
            {
                _notify.ShowError(ex.Message);
            }
        }

        private void FillBaseInfo()
        {
            Dispatcher.Invoke(() =>
            {
                _triggerButtonsStatus(false);

                TbDeviceName.Value = _info.Name;

                TbDeviceType.Value = _info.Type;
                TbDeviceCode.Value = _info.Device;
                TbProductionYear.Value = _info.Year;
                TbProductionCode.Value = _info.Sn;
                CbEnableDs.IsChecked = _info.DataShield;
                CbEnableGmp.IsChecked = _info.Gmp;
                CbEnableUnitSys.IsChecked = _info.Unit;
                CbEnableAutoReport.IsChecked = _info.AutoReport;
                IudTestId.Value = _info.LastTestId + "";

                _triggerButtonsStatus(true);
            });
        }

        private void _triggerButtonsStatus(bool status)
        {
            BtnOk.IsEnabled = status;
            Loading.Visibility = status ? Visibility.Collapsed : Visibility.Visible;
        }
    }
}
